#include "routeplan.h"
#include "station.h"
#include <QLineEdit>
#include <QLabel>
#include <QPushButton>
#include <QPixmap>
#include <QCheckBox>
#include <QMessageBox>
#include <QCompleter>
#include <QStringList>
#include <QString>
#include <QDebug>

RoutePlan::RoutePlan(QWidget *parent)
    : QMainWindow{parent}
{
    QPixmap headPicture(":/new/prefix1/resource/ReferenceOnly.png");
    QLabel *head = new QLabel(this);
    head->setFixedSize(headPicture.size());
    head->setPixmap(headPicture);

    //设置输入界面
    QLabel *depatureLable = new QLabel("出发车站：",this);
    depatureLable->move(50,100);
    QLineEdit *depatureEdit = new QLineEdit(this);
    depatureEdit->move(110,100);
    QLabel *destinationLable = new QLabel("到达车站：",this);
    destinationLable->move(270,100);
    QLineEdit *destinationEdit = new QLineEdit(this);
    destinationEdit->move(330,100);
    QCheckBox*checkBox = new QCheckBox("出站换乘/虚拟换乘",this);
    checkBox->move(50,150);
    checkBox->setFixedWidth(135);
    checkBox->setTristate(false);
    QPushButton*startQuery = new QPushButton("查询",this);
    startQuery->move(500,100);

    //输入列表
    QStringList wordList;
    wordList<< "刘园" << "瑞景新苑" << "佳园里" << "本溪路" << "勤俭道"
             << "洪湖里" << "西站" << "西北角" << "西南角" << "二纬路"
             << "海光寺" << "鞍山道" << "营口道" << "小白楼" << "下瓦房"
             << "南楼" << "土城" << "陈塘庄" << "复兴门" << "华山里"
             << "财经大学" << "双林" << "李楼" << "梨双路北（暂缓开通）" << "洪泥河东"
             << "高庄子" << "上郭庄（暂缓开通）" << "国家会展中心" << "国瑞路" << "东沽路"
             << "咸水沽北（暂缓开通）" << "双桥河（暂缓开通）"
             << "曹庄" << "卞兴" << "芥园西道" << "咸阳路" << "长虹公园"
             << "广开四马路" << "鼓楼" << "东南角" << "建国道" << "天津站"
             << "远洋国际中心" << "顺驰桥" << "靖江路" << "翠阜新村" << "屿东城"
             << "登州路" << "国山路" << "空港经济区" << "滨海国际机场" << "小淀"
             << "丰产河" << "华北集团" << "天士力" << "宜兴埠" << "张兴庄"
             << "铁东路" << "北站" << "中山路" << "金狮桥" << "津湾广场"
             << "和平路" << "西康路" << "吴家窑" << "天塔" << "周邓纪念馆"
             << "红旗南路" << "王顶堤" << "华苑" << "大学城" << "高新区"
             << "学府工业区" << "杨伍庄" << "南站" << "新兴村" << "民航大学"
             << "航双路" << "跃进北路" << "登州南路" << "沙柳南路" << "万东路"
             << "泰昌路" << "成林道" << "六纬路" << "徐州道" //此“和平路”应去重复<< "和平路"
             << "金街" << "北辰科技园北" << "丹河北道" << "北辰道" << "职业大学"
             << "淮河道" << "辽河北道" << "宜兴埠北" << "志成路" << "思源路"
             << "建昌道" << "金钟河大街" << "月牙河" << "幸福公园" << "津塘路"
             << "直沽" << "西南楼" << "文化中心" << "天津宾馆" << "肿瘤医院"
             << "体育中心" << "凌宾路" << "昌凌路" << "中医一附院" << "李七庄南"
             << "海光寺" << "南孙庄" << "南何庄" << "大毕庄" << "金钟街"
             << "徐庄子" << "民权门" << "北宁公园" << "新开河" << "外院附中"
             << "天泰路" << "北运河（暂缓开通）" << "北竹林" << "复兴路" << "人民医院" << "宜宾道"
             << "鞍山西道" << "天拖" << "一中心医院" << "迎风道" << "南翠屏"
             << "水上公园东路" << "乐园道" << "尖山路" << "医大二院" << "梅江道"
             << "左江道" << "梅江公园" << "梅江会展中心" << "解放南路" << "洞庭路"
             << "梅林路" << "渌水道" << "咸水沽西" << "和慧南路" << "南开大学津南校区"
             << "海河教育园区" << "天津大学北洋园校区" << "景荔道" << "景荷道" << "双港"
             << "大王庄" << "十一经路" << "东兴路" << "中山门" << "一号桥"
             << "二号桥" << "张贵庄" << "新立" << "东丽开发区" << "小东庄"
             << "军粮城" << "钢管公司" << "胡家园" << "塘沽" << "泰达"
             << "市民广场" << "太湖路" << "会展中心" << "东海路" << "崂山道"
             << "香山道" << "万山道" << "方山道" << "金贸产业园" << "龙涵道"
             << "环宇道" << "柳林路" << "微山路" << "玛钢厂" << "春海路"
             << "南珠桥" << "友谊南路" << "江湾二支路" << "丽江道" << "瑶环路"
             << "于台" << "东丽六经路" << "东丽三经路" << "东丽一经路" << "驯海路"
             << "东丽文体中心" << "招远路" << "雪莲南路" << "海河东路" << "学苑北路"
             << "东江道";
    QCompleter *completer = new QCompleter(wordList, this);
    depatureEdit->setCompleter(completer);
    destinationEdit->setCompleter(completer);
    completer->setCaseSensitivity(Qt::CaseInsensitive);

    //判断选择框状态
    connect(checkBox, &QCheckBox::stateChanged, this, &RoutePlan::onCheckBoxStateChanged);

    setDistance01();
    setPath();
    updateDistancePath();

    //以下设置多个QLabel用来面向用户输出
    QLabel *title=new QLabel("以下方案可供参考：",this);
    QLabel *step1=new QLabel(this);
    QLabel *step2=new QLabel(this);
    QLabel *step3=new QLabel(this);
    QLabel *step4=new QLabel(this);
    QLabel *step5=new QLabel(this);
    QLabel *step6=new QLabel(this);
    QLabel *step7=new QLabel(this);
    QLabel *step8=new QLabel(this);
    QLabel *step9=new QLabel(this);
    QLabel *step10=new QLabel(this);
    QLabel *step[10]={step1,step2,step3,step4,step5,step6,step7,step8,step9,step10};
    title->move(50,200);
    title->adjustSize();
    step1->move(50,225);
    step2->move(50,250);
    step3->move(50,275);
    step4->move(50,300);
    step5->move(50,325);
    step6->move(50,350);
    step7->move(50,375);
    step8->move(50,400);
    step9->move(50,425);
    step10->move(50,450);

    //设置一些QString
    QString str1=" 乘坐";
    QString str2="号线 ";
    QString str3="至 ";
    QString str4="换乘";
    QString str5="出站步行 ";
    QString str6="出站步行 至 3号线";
    QString str7="出站步行 至 4号线";

    //设置输出与错误提示，注意函数参数从QString转换到std::string
    connect(startQuery,&QPushButton::clicked,this,[=](){//pay attention
        if(!stationExist(depatureEdit->text().toStdString(),destinationEdit->text().toStdString())){
            QMessageBox::critical(this,"错误","请输入正确的的站名！");
        }
        else if(stationIdentical(depatureEdit->text().toStdString(),destinationEdit->text().toStdString())){
            QMessageBox::critical(this,"错误","出发车站和到达车站不能相同！");
        }
        else{
            //以下生成路径
            int depatureStation;
            int destinationStation;
            if(depatureEdit->text() == "和平路"){
                destinationStation = findStation(destinationEdit->text().toStdString());
                depatureStation = whichHepingluDeparture(destinationStation);
            }
            else if(destinationEdit->text() == "和平路"){
                depatureStation =findStation(depatureEdit->text().toStdString());
                destinationStation = whichHepingluDeparture(depatureStation);
            }
            else{
                depatureStation = findStation(depatureEdit->text().toStdString());
                destinationStation = findStation(destinationEdit->text().toStdString());
            }
            int *routeReverse = Query(depatureStation,destinationStation);//此时得到的是最终路径的反序
            int *route=new int[routeArrayLength];
            for(int i = routeArrayLength - 1;i >= 0;i--){
                route[i] = *routeReverse++;
            }//将routeReverse反序得到真正的路径并储存在route中

            //以下显示路径
            if(routeArrayLength == 2){
                QString tempStr1 = QString::number(getLine(depatureStation,destinationStation))+str2;
                if((route[0] == 100||route[0] == 101||route[0] == 102)&&(route[1] == 100||route[1] == 101||route[1] == 102)){
                    tempStr1 = "5号线/6号线 ";
                }//出发站和到达站为肿瘤医院、天津宾馆、文化中心的情况
                for(int i = 0;i < 10;i++){
                    step[i]->setText("");//多次查询时，要清除前一次查询的内容
                }
                if(getLine(depatureStation,destinationStation) == -1){
                    step1->setText(QString::fromStdString(stationArray[depatureStation].m_Name)+" 步行 至 "
                                   +QString::fromStdString(stationArray[destinationStation].m_Name));
                }//若getLine(depatureStation,destinationStation) == -1，则直接步行至虚拟换乘的另一个车站
                else{
                    step1->setText(QString::fromStdString(stationArray[depatureStation].m_Name)+str1
                                   +tempStr1+str3+QString::fromStdString(stationArray[destinationStation].m_Name));
                }
                step1->adjustSize();
            }//若routeArrayLength=2，则不需要判断换乘，直接输出
            else{
                int countTransfer = 0;
                QString stringArray[10];
                if(viaTianjinbinguan(route,routeArrayLength)){
                    if(getLine(depatureStation,route[1]) == -1){
                        stringArray[0]=QString::fromStdString(stationArray[depatureStation].m_Name)+" 步行 至 ";
                    }//若getLine(depatureStation,route[1]) == -1，则先要步行至虚拟换乘的另一个车站
                    else{
                        stringArray[0]=QString::fromStdString(stationArray[depatureStation].m_Name)
                                         +str1+QString::number(getLine(depatureStation,route[1]))+str2+str3;
                    }
                    for(int i = 0;i < routeArrayLength - 2;i++){
                        if(routeArrayLength == 3
                            &&((route[i] == 102&&route[i + 1] == 101&&route[i + 2] == 100)
                                ||(route[i] == 100&&route[i + 1] == 101&&route[i + 2] == 102))){
                            QString tempStr2 = "5号线/6号线 ";
                            stringArray[0]=QString::fromStdString(stationArray[depatureStation].m_Name)+str1+tempStr2+str3;
                            break;
                        }//出发站和到达站为肿瘤医院、文化中心的情况

                        //以下8种特殊情况
                        if(route[i] == 128&&route[i + 1] == 100&&route[i + 2] == 99){
                            stringArray[countTransfer]+="天津宾馆（同台换乘）/文化中心";
                            stringArray[countTransfer + 1]=str4+QString::number(getLine(route[i + 1],route[i + 2]))+str2+str3;
                            countTransfer++;
                            continue;
                        }//乐园道-文化中心-西南楼
                        if(route[i] == 99&&route[i + 1] == 100&&route[i + 2] == 128){
                            stringArray[countTransfer]+="天津宾馆（同台换乘）/文化中心";
                            stringArray[countTransfer + 1]=str4+QString::number(getLine(route[i + 1],route[i + 2]))+str2+str3;
                            countTransfer++;
                            continue;
                        }//西南楼-文化中心-乐园道
                        if(route[i] == 103&&route[i + 1] == 102&&route[i + 2] == 127){
                            stringArray[countTransfer]+="天津宾馆（同台换乘）/肿瘤医院";
                            stringArray[countTransfer + 1]=str4+QString::number(getLine(route[i + 1],route[i + 2]))+str2+str3;
                            countTransfer++;
                            continue;
                        }//体育中心-肿瘤医院-水上公园东路
                        if(route[i] == 127&&route[i + 1] == 102&&route[i + 2] == 103){
                            stringArray[countTransfer]+="天津宾馆（同台换乘）/肿瘤医院";
                            stringArray[countTransfer + 1]=str4+QString::number(getLine(route[i + 1],route[i + 2]))+str2+str3;
                            countTransfer++;
                            continue;
                        }//水上公园东路-肿瘤医院-体育中心
                        if(route[i] == 103&&route[i + 1] == 102&&route[i + 2] == 101&&route[i + 3] == 100&&route[i + 4] == 128){
                            stringArray[countTransfer]+="肿瘤医院（同台换乘）";
                            stringArray[countTransfer + 1]=str4+QString::number(getLine(route[i + 3],route[i + 4]))+str2+str3;
                            countTransfer++;
                            continue;
                        }//体育中心-肿瘤医院-天津宾馆-文化中心-乐园道
                        if(route[i] == 128&&route[i + 1] == 100&&route[i + 2] == 101&&route[i + 3] == 102&&route[i + 4] == 103){
                            stringArray[countTransfer]+="肿瘤医院（同台换乘）";
                            stringArray[countTransfer + 1]=str4+QString::number(getLine(route[i + 3],route[i + 4]))+str2+str3;
                            countTransfer++;
                            continue;
                        }//乐园道-文化中心-天津宾馆-肿瘤医院-体育中心
                        if(route[i] == 127&&route[i + 1] == 102&&route[i + 2] == 101&&route[i + 3] == 100&&route[i + 4] == 99){
                            stringArray[countTransfer]+="肿瘤医院（同台换乘）";
                            stringArray[countTransfer + 1]=str4+QString::number(getLine(route[i + 3],route[i + 4]))+str2+str3;
                            countTransfer++;
                            continue;
                        }//水上公园东路-肿瘤医院-天津宾馆-文化中心-西南楼
                        if(route[i] == 99&&route[i + 1] == 100&&route[i + 2] == 101&&route[i + 3] == 102&&route[i + 4] == 127){
                            stringArray[countTransfer]+="肿瘤医院（同台换乘）";
                            stringArray[countTransfer + 1]=str4+QString::number(getLine(route[i + 3],route[i + 4]))+str2+str3;
                            countTransfer++;
                            continue;
                        }//西南楼-文化中心-天津宾馆-肿瘤医院-水上公园东路

                        if(judgeTransfer(route[i],route[i + 2])){
                            stringArray[countTransfer]+=QString::fromStdString(stationArray[route[i + 1]].m_Name);
                            if(getLine(route[i + 1],route[i + 2]) == -1){
                                switch(route[i + 1]){
                                case 13:
                                case 81:
                                case 132:
                                case 178:
                                case 151:
                                case 171:
                                case 19:
                                case 192:
                                    stringArray[countTransfer + 1]=str5+str3;
                                    break;
                                case 58:
                                    stringArray[countTransfer + 1]=str7;
                                    break;
                                case 82:
                                    stringArray[countTransfer + 1]=str6;
                                    break;
                                }
                            }//虚拟换乘/出站换乘
                            else{
                                stringArray[countTransfer + 1]=str4+QString::number(getLine(route[i + 1],route[i + 2]))+str2+str3;
                            }//正常换乘
                            countTransfer++;
                        }//判断是否有换乘
                    }
                    stringArray[countTransfer]+=QString::fromStdString(stationArray[destinationStation].m_Name);
                }//若经过天津宾馆，需单独判断
                else{
                    if(getLine(depatureStation,route[1]) == -1){
                        stringArray[0]=QString::fromStdString(stationArray[depatureStation].m_Name)+" 步行 至 ";
                    }//若getLine(depatureStation,route[1]) == -1，则先要步行至虚拟换乘的另一个车站
                    else{
                        stringArray[0]=QString::fromStdString(stationArray[depatureStation].m_Name)
                                         +str1+QString::number(getLine(depatureStation,route[1]))+str2+str3;
                    }
                    for(int i = 0;i < routeArrayLength - 2;i++){
                        //以下4种特殊情况
                        if(route[i] == 128&&route[i + 1] == 100&&route[i + 2] == 99){
                            stringArray[countTransfer]+="天津宾馆（同台换乘）/文化中心";
                            stringArray[countTransfer + 1]=str4+QString::number(getLine(route[i + 1],route[i + 2]))+str2+str3;
                            countTransfer++;
                            continue;
                        }//乐园道-文化中心-西南楼
                        if(route[i] == 99&&route[i + 1] == 100&&route[i + 2] == 128){
                            stringArray[countTransfer]+="天津宾馆（同台换乘）/文化中心";
                            stringArray[countTransfer + 1]=str4+QString::number(getLine(route[i + 1],route[i + 2]))+str2+str3;
                            countTransfer++;
                            continue;
                        }//西南楼-文化中心-乐园道
                        if(route[i] == 103&&route[i + 1] == 102&&route[i + 2] == 127){
                            stringArray[countTransfer]+="天津宾馆（同台换乘）/肿瘤医院";
                            stringArray[countTransfer + 1]=str4+QString::number(getLine(route[i + 1],route[i + 2]))+str2+str3;
                            countTransfer++;
                            continue;
                        }//体育中心-肿瘤医院-水上公园东路
                        if(route[i] == 127&&route[i + 1] == 102&&route[i + 2] == 103){
                            stringArray[countTransfer]+="天津宾馆（同台换乘）/肿瘤医院";
                            stringArray[countTransfer + 1]=str4+QString::number(getLine(route[i + 1],route[i + 2]))+str2+str3;
                            countTransfer++;
                            continue;
                        }//水上公园东路-肿瘤医院-体育中心

                        if(judgeTransfer(route[i],route[i + 2])){
                            stringArray[countTransfer]+=QString::fromStdString(stationArray[route[i + 1]].m_Name);
                            if(getLine(route[i + 1],route[i + 2]) == -1){
                                switch(route[i + 1]){
                                case 13:
                                case 81:
                                case 132:
                                case 178:
                                case 151:
                                case 171:
                                case 19:
                                case 192:
                                    stringArray[countTransfer + 1]=str5+str3;
                                    break;
                                case 58:
                                    stringArray[countTransfer + 1]=str7;
                                    break;
                                case 82:
                                    stringArray[countTransfer + 1]=str6;
                                    break;
                                }
                            }//虚拟换乘/出站换乘
                            else{
                                stringArray[countTransfer + 1]=str4+QString::number(getLine(route[i + 1],route[i + 2]))+str2+str3;
                            }//正常换乘
                            countTransfer++;
                        }//判断是否有换乘
                    }
                    stringArray[countTransfer]+=QString::fromStdString(stationArray[destinationStation].m_Name);
                }//不经过天津宾馆
                for(int i = 0;i < 10;i++){
                    step[i]->setText("");//多次查询时，要清除前一次查询的内容
                }
                for(int i = 0;i < countTransfer + 1;i++){
                    step[i]->setText(stringArray[i]);
                    step[i]->adjustSize();
                }
            }
        }
    });
}

//判断选择框状态函数
void RoutePlan::onCheckBoxStateChanged(int state) {
    if (state == Qt::Checked) {
        setDistance01();//▲注意
        setDistance02();
        setPath();//▲注意
        updateDistancePath();
    }
    else if (state == Qt::Unchecked) {
        setDistance01();
        setPath();//▲注意
        updateDistancePath();
    }
}
